// mircoApp/agentShop/agentShop_setting/agentShop_setting.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: [],
    avatar: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  inpshop_name(e) {
    this.setData({
      'infoData.shop_name': e.detail.value
    })
  },
  async inpVal(e) {
    let key = e.currentTarget.dataset.key;
    let name = `infoData.${key}`;
    this.setData({
      [name]: e.detail
    })
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.agent-shop.frontend.agent.manage"),
    })
    if (!data.result) return app.tips(data.msg);
    this.setData({
      infoData: data.data,
      avatar: data.data.avatar_image,
      avatar_image: data.data.avatar_image
    })
  },
  async save() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.agent-shop.frontend.agent.edit"),
      data: {
        avatar: this.data.avatar,
        shop_name: this.data.infoData.shop_name,
        shop_desc: this.data.infoData.shop_desc
      },
    })
    app.tips(data.msg)
  },
  //头像上传
  afterRead(event) {
    wx.showLoading({
      title: '上传中',
    })
    let {
      file
    } = event.detail;
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.setData({
          'infoData.avatar_image': _res.data.img_url,
          avatar: _res.data.img
        })
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
})